/**
 * 		common
 * 
 * 	this is file content function for shared
 * 	History:
 * 
 * 	DATE 					AUTHOR			 		   DESCRIPTION
 * ---------------------------------------------------------------------
 * 	08-Dec-2012 			NguyenNV 				   Create new
 * 	06-Feb-2013				DatBP					   Add new CommonGis	
 *  25-Feb-2013				DatBP					   Add new daysInMonth function
 */

/*
 * 
 * Function: displayContentInfor	 											   							
 * Parameters:
 * 		 Parameter1: String 															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	Draw content on popup 
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
function displayContentInfor(_typePopup){
	
	var divContent = base.getElementById(DIV_POPUP_CONTENT_INFOR);
	switch(_typePopup)
	{
		case Popup_Type.Display_Poligon_Contain_Point:
			SearchingAction.drawContentPopup(divContent);
			break;
		case Popup_Type.Display_Detail_Infor:
			waitingDataAndDraw();		// waiting and draw on popup
			break;
		default:
			alert("No content for display !");
			break;
	}
}
/*
 * 
 * Function: isLineInDistrict	 											   							
 * Parameters:
 * 		 Parameter1: String
 * 		 Parameter2: String 															 																				
 * Return:	 
 * 		 void : true (if the line inside QuanHuyen)
 * 				false (else)
 * 
 * Description:	check line is or not inside district  
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
function isLineInDistrict(_lineGeom,_idQuanHuyen ){
	
	var lineTest = _lineGeom;
	var pointTest = null;
	var j=0;
	var k=0;
	var _featureDistrict = null;
	// tao mang xem co nam trong hay ko
	var IsIn = [];	
	
	for(var i=0; i<arrFeatureDistrict.length; i++)
	{
		if(arrFeatureDistrict[i].fid == _idQuanHuyen)
		{
			_featureDistrict = arrFeatureDistrict[i];
		}
	}
	
	for(j=0;j<lineTest.components.length;j++)
	{
		var lineComponents = lineTest.components[j];
		IsIn[j] = [];
		
		for(k = 0; k < lineComponents.components.length; k++)
		{
			IsIn[j][k] = false;
		}
	}
		var geom = _featureDistrict.geometry.components[0];
		// neu multiline thi components > 1
		for(j=0; j<lineTest.components.length; j++)
		{
			var lineComponents = lineTest.components[j];
			for(k=0;k<lineComponents.components.length;k++)
			{
				pointTest = lineComponents.components[k];
				if(geom.containsPoint(pointTest))
				{
					IsIn[j][k] = true;
					//break;
				}
			}
		}
		
	var bIsIn = true;
	for(var i=0;i<IsIn.length;i++)
	{
		for(k=0;k<IsIn[i].length;k++)
		{
			if(!IsIn[i][k])
			{
				bIsIn = false;
				break;
			}
		}
		if(!bIsIn) break;
	}
	return bIsIn;
}
/*
 * 
 * Function: isPointInDistrict	 											   							
 * Parameters:
 * 		 Parameter1: String
 * 		 Parameter2: String 															 																				
 * Return:	 
 * 		 void : true (if the point inside QuanHuyen)
 * 				false (else)
 * 
 * Description:	Draw content on popup 
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
function isPointInDistrict(_idQuan, _geoPoint){
	var IsIn = false;
	for(var i=0; i<arrFeatureDistrict.length; i++)
	{
		if(arrFeatureDistrict[i].fid == _idQuan)
		{
			var geom = arrFeatureDistrict[i].geometry.components[0];
			if(geom.containsPoint(_geoPoint))
			{
				IsIn = true;
				break;
			}
		}
	}
	return IsIn;
}
/*
 * START CLASS CommonFuns
 */

CommonFuns = {
		

/*
* Function Name 	| 			removeByValue				   									   	    
*------------------------------------------------------------------------------------------------------
* Parameters		| Parameter1 : Array																
* 					| Parameter2 : String																
*------------------------------------------------------------------------------------------------------
* Return			| true : contain the value															
* 					| false: do not contain the value    												
*------------------------------------------------------------------------------------------------------
* Description		| Remove an element out of array		 											
*------------------------------------------------------------------------------------------------------
* Authors			| NguyenNV																			
* Date				| 2012/12/03																		
*/
removeByValue:function(arr, val) {
	for(var i=0; i<arr.length; i++) {
		if(arr[i] == val) {
			arr.splice(i, 1);
			break;
		}
	}
},
removeOptionCombo:function(strId){
	var combo = base.getElementById(strId);
	if (combo.length > 0){
		 while(combo.length > 0)
		  {
			 combo.remove(combo.length-1);
		  }
	}
},
/*------------------------------------------------------------------------------------------------------
 * Function Name 	| 			isExistItem			   									   	    
 *------------------------------------------------------------------------------------------------------
 * Parameters		| Parameter1 : Array																
 * 					| Parameter2 : String																
 *------------------------------------------------------------------------------------------------------
 * Return			| true : contain the value															
 * 					| false: do not contain the value    												
 *------------------------------------------------------------------------------------------------------
 * Description		| Checking this array contain the values 											
 *------------------------------------------------------------------------------------------------------
 * Authors			| NguyenNV																			
 * Date				| 2012/12/03																		
 */
isExistItem:function(_array, values_)
{
	for(var i=0; i <_array.length ; i++)
		{
			if(_array[i] == values_)
				return true;
		}
	return false;
	
},
/*------------------------------------------------------------------------------------------------------
 * Function Name 	| 			insertToArray			   									   	    
 *------------------------------------------------------------------------------------------------------
 * Parameters		| Parameter1 : Array																
 * 					| Parameter2 : String																
 *------------------------------------------------------------------------------------------------------
 * Return			| true : contain the value															
 * 					| false: do not contain the value    												
 *------------------------------------------------------------------------------------------------------
 * Description		| Checking this array contain the values 											
 *------------------------------------------------------------------------------------------------------
 * Authors			| NguyenNV																			
 * Date				| 2012/12/03																		
 */
insertToArray:function(_value, _array){

	if(_value != null)
	_array[_array.length] = _value;
	
},
/*------------------------------------------------------------------------------------------------------
 * Function Name 	| 			checkExistAndInsert					   									   	     
 *------------------------------------------------------------------------------------------------------ 
 * Parameters		| Parameter1 : Array																 
 * 					| Parameter2 : String																 
 *------------------------------------------------------------------------------------------------------ 
 * Return			| true (not exist and inserted)
 * 					| false (exist and not inserted)																				 
 *------------------------------------------------------------------------------------------------------ 
 * Description		| Insert value to array																 
 *------------------------------------------------------------------------------------------------------ 
 * Author			| NGUYENNV																			 
 * Date 			| 2012/12/03	
 *------------------------------------------------------------------------------------------------------ 								   								 	      	    					
 */
checkExistAndInsert:function(_arrOut,_value)
{
		if(this.isExistItem(_arrOut ,_value)==false)
			{
				
				_arrOut[_arrOut.length] = _value;
				return true;
			}
		return false;
},
getDateToday:function(){
	var currentTime = new Date();
			var year = currentTime.getFullYear();
			var month = currentTime.getMonth() + 1;
			var day = currentTime.getDate();
			if(month<10) month = "0" + month;
			if(day<10) day = "0" + day;
			var date = year + "-" + month + "-" + day;
			return date;
},
/* Function: compareDate	 											   							
 * Parameters:
 * 		 enDate1 yyyy/mm/dd
 * 		 enDate2 yyyy/mm/dd	
 * Return:	 
 * 		 0 : enDate1 <= enDate2
 * 		 1 : enDate1 > enDate2 
 * Author: NGUYENNV
 * Date : 2012/11/26										   												
 * Description:	comparing date and date					          	    					
/*------------------------------------------------------------------------------------------------------*/
compareDate:function(enDate1, enDate2)
{
	
	var year1 =enDate1.split("-")[0] ; 
	var month1 =enDate1.split("-")[1]; 
	var day1 = enDate1.split("-")[2];
	
	var year2 =enDate2.split("-")[0] ; 
	var month2 =enDate2.split("-")[1]; 
	var day2 = enDate2.split("-")[2];
	
	var result=0;
	if(year1 > year2) result = 1;
	else if(year1 == year2)
		{
			if(month1 > month2)  result = 1;
			else if(month1 == month2)
				{
					if(day1 >= day2)  result = 1;
					else  result = 0;
				}
				else  result = 0;
		}
	else  result = 0;
	return  result;
},
/*
 * 
 * Function: getDistrictByGeom	 											   							
 * Parameters:
 * 		 Parameter1: array 															 																				
 * Return:	 
 * 		 void : string
 * 
 * Description:	get location district by geom
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getDistrictByGeom:function(_geometry){
	
	var districtId = null;
	for(var i=0; i<arrFeatureDistrict.length; i++)
	{
		var geom = arrFeatureDistrict[i].geometry.components[0];
		if(geom.containsPoint(_geometry))
		{
			var featureDistrict = arrFeatureDistrict[i];
			districtId = featureDistrict.attributes["dist_id"];
			break;
		}
	}
	return districtId;
},
/*
 * 
 * Function: getCommunByGeom	 											   							
 * Parameters:
 * 		 Parameter1: array 															 																				
 * Return:	 
 * 		 void : string
 * 
 * Description:	get location commun by geom
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getCommunByGeom:function(_geometry){
	var districtId = null;
	for ( var i = 0; i < arrFeatureCommune.length; i++) {
		var geom = arrFeatureCommune[i].geometry.components[0];
		if(geom.containsPoint(_geometry))
		{
			var featureDistrict = arrFeatureCommune[i];
			districtId = featureDistrict.attributes["com_id"];
			
			break;
		}
	}
	return districtId;
},
/*
 * 
 * Function: getDistricName	 											   							
 * Parameters:
 * 		 Parameter1: array 															 																				
 * Return:	 
 * 		 void : string
 * 
 * Description:	get district name by geom
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getDistricName:function(maQuanHuyen){
	var nameVn = "";
	for(var i=0; i< arrFeatureDistrict.length; i++)
	{
		if(maQuanHuyen == arrFeatureDistrict[i].attributes["dist_id"])
			{
				nameVn = arrFeatureDistrict[i].attributes["name_vn"];
				break;
			}
	}
	return nameVn;
},
/*
 * 
 * Function: getCommunName	 											   							
 * Parameters:
 * 		 Parameter1: array 															 																				
 * Return:	 
 * 		 void : string
 * 
 * Description:	get commun name by geom
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getCommunName:function(maPhuongXa){
	var nameVn = "";
	for(var i=0; i< arrFeatureCommune.length; i++)
	{
				if(maPhuongXa == arrFeatureCommune[i].attributes["com_id"])
					{
						nameVn = arrFeatureCommune[i].attributes["name_vn"];
						break;
					}
	}
	return nameVn;
},
/* Function: checkDateBetweenRank	 											   							
 * Parameters:
 * 		 enDateSearch yyyy/mm/dd
 * 		 fromDate     yyyy/mm/dd
 * 		 toDate       yyyy/mm/dd	
 * Return:	 
 * 		 true : fromDate <= enDateSearch <= toDate
 * 		 false : enDateSearch > toDate ||  enDateSearch < fromDate
 * Author: NGUYENNV
 * Date : 2012/11/26										   												
 * Description:	checking the searchingDate is between fromDate and toDate					          	    					
/*------------------------------------------------------------------------------------------------------*/
checkDateBetweenRank:function(enDateSearch, fromDate, toDate){
	
	var result = false;
	
	if(fromDate == "" && toDate != ""){
		//alert("01");
		if(this.compareDate(enDateSearch, toDate) == 0)
			result = true;
	}
	else if(fromDate != "" && toDate == ""){
		//alert("02");
		if(this.compareDate(enDateSearch, fromDate) == 1)
			result = true;
	}
	else if(fromDate != "" && toDate != "")
		{
			//alert("03");
			if(this.compareDate(enDateSearch, fromDate) == 1 && this.compareDate(enDateSearch, toDate) == 0)
				result = true;
		}
	else result = false;
	return result;
},
/*
 * 
 * Function: FillQuanHuyenPX	 											   							
 * Parameters:
 * 		 Parameter1: array 															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	get list quan huyen, phuong xa
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
FillQuanHuyenPX:function(_array){
	
	for ( var i = 0; i < CurrentLayer.arrAlias.length; i++) 
	{
		
		if(CurrentLayer.arrAlias[i].Name == "maquan" 
			|| CurrentLayer.arrAlias[i].Name == "maphuong")
			{	
				var temp = CurrentLayer.arrAlias[i].Name;
				for(var j = 0;j < _array.length; j++)
				{
					var idLocation = _array[j].attribute[temp];
					if(idLocation.length > 0)
					if(temp == "maquan") 
						{
							var _distric = this.getDistricName(idLocation);
							_array[j].attribute[temp] = _distric;
						}
					else{
							var _commun = this.getCommunName(idLocation);
							_array[j].attribute[temp] = _commun;
						}
				}
			}
	}
},
FillDataToArray:function(_array)
{
	IS_READING_DATA=0;
	COMPLETED_WAITING=0;
	
	var sFileJSP = "GetData.jsp";
	for ( var i = 0; i < CurrentLayer.arrAlias.length; i++) 
	{
		if(CurrentLayer.arrAlias[i].IsCategory)
			{
					COMPLETED_WAITING++;
					var $divMap_Data = $(document.createElement('div'));
					$divMap_Data.attr("Name", CurrentLayer.arrAlias[i].Name);
					$divMap_Data.load(
							sPortletJSP + sFileJSP + "?attr=" + CurrentLayer.arrAlias[i].FieldValue
							+"&asset="+CurrentLayer.layerId
							+"&table="+CurrentLayer.arrAlias[i].TableName,
						function(data) {
							var temp = 	$(this).attr("Name");
							var splitData = data.split('<p>');
							var ID_DanhMuc = "";
							var CODE_DanhMuc = "";
							
							for(var j = 0;j < _array.length; j++)
							{
								for ( var iItem = 0; iItem < splitData.length; iItem++) 
								{
									var splitData_Item = splitData[iItem].split('<br>');
									if (splitData_Item.length < 2) continue;
									if(splitData_Item[0]==null)
										{
											splitData_Item[0] = "";
											splitData_Item[1] = "";
										}
									if(splitData_Item[1]==null)
										splitData_Item[1] = "";
								
									ID_DanhMuc = splitData_Item[0];
									CODE_DanhMuc = splitData_Item[1];
									if(_array[j].attribute[temp] == ID_DanhMuc.replace(/^\s+|\s+$/g, ""))
									{
											_array[j].attribute[temp] = CODE_DanhMuc.trim();
									}
								}						
							}
							IS_READING_DATA++;
						});
			
			}
		
	}
	for ( var i = 0; i < CurrentLayer.arrAlias.length; i++) 
	{
		if(CurrentLayer.arrAlias[i].IsShow && CurrentLayer.arrAlias[i].Type =="date")
		{
		
		date = CurrentLayer.arrAlias[i].Name;
		for(var j = 0;j < _array.length; j++)
			{
				enDate = _array[j].attribute[date];
				if(enDate.length > 1)
					{
					var year = enDate.substr(0, 4); 
					var month = enDate.substr(5, 2); 
					var day = enDate.substr(8, 2);
					var vnDate = day+"-"+month+"-"+year;
					_array[j].attribute[date] = vnDate;
					}
			}
		}
	}
	this.FillQuanHuyenPX(_array);
},
/*
 * 
 * Function: caculateNumOfDayFromNow	 											   							
 * Parameters:
 * 		 Parameter1: Date (yyyy-mm-dd) 															 																				
 * Return: int 
 * 
 * Description:	Calculate num of day from date input to now
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
caculateNumOfDayFromNow:function(engDate){
	var totalDays = -1;
	if(engDate.length == 10){
		var year 	= parseInt(engDate.substr(0, 4)); 
		var month 	= parseInt(engDate.substr(5, 2)); 
		var date 	= parseInt(engDate.substr(8, 2));
		var fromDate = new Date(year, month, date);
		var now = new Date();
		var time  = now.getTime() - fromDate.getTime();
		totalDays = Math.abs(Math.round(time / 1000 / 60 / 60 / 24));
	}
	return totalDays;
},
/* START DATBP ADD NEW */
showTableCmonContent:function(){
	$('table#cmon_tbl_result').show();
	$('#pageNavPosition').show();
},

hideTableCmonContent:function (){
	$('table#cmon_tbl_result').hide();
	$('#pageNavPosition').hide();
},
/* END DATBP ADD NEW */

/* END DUYLIEM ADD NEW */
/* NGUYENNV show/hide tim kiem chung*/
// START NGUYENNV add new 
showTimKiemChungVHBD:function(){
	$('#content_list_bongden').show();
	$('#div_search_thongtin_phkh').hide();
	$('#div_SearchNgap').hide();
	$("#div_search_attribute").hide();
},
hideTimKiemChungVHBD:function(){
	$('#content_list_bongden').hide();
}
//END NGUYENNV add new 
};
/*
 * END CLASS CommonFuns
 */
/*	
 * 
 * START CLASS CommonGeo 
 * NGUYENNV
 */
CommonGeo = {
	
zoomToDistrict:function(_idDistrict){
	var feature = null;
	for(var i=0 ; i < vectorDistricts.features.length ; i++ ){
		if(vectorDistricts.features[i].attributes['dist_id'] == _idDistrict)
			{
				feature = vectorDistricts.features[i];
				break;
			}
	}
	CommonGeo.zoomToPolygon(feature);
},
zoomToCommun:function(_idCommun){
	var feature = null;
	var _bounds = null;
	for(var i=0 ; i < vectorCommune.features.length ; i++ ){
		if(vectorCommune.features[i].attributes['com_id'] == _idCommun)
			{
				feature = vectorCommune.features[i];
				break;
			}
	}
	CommonGeo.zoomToPolygon(feature);
},	
zoomToPolygon:function(feature){
	var _bounds = null;
	if(feature != null)
	{
		//vectorDistricts.drawFeature(feature, "select");
		var styleDefault = {
			    strokeColor: COLOR_BORDER_POLYGON,
			    strokeOpacity: "0.8",
			    strokeWidth: 2,
			    fillColor: COLOR_UNSELECTED_FILL_POLYGON,
			    fillOpacity: 0.3,
			    pointRadius: 5,
			    cursor: "pointer"
		};
		createPolygonFromGeometry(vectorGraphics, feature.geometry, styleDefault);
		if (_bounds == null)
			_bounds = feature.geometry.getBounds().clone();
			_bounds.extend(feature.geometry.getBounds());
		if (_bounds != null)
				map.zoomToExtent(_bounds, false);
	}
},
/*
 * 
 * Function: displayPopup	 											   							
 * Parameters:
 * 		 Parameter1: String 
 * 		 Parameter2: Int
 * 		 Parameter3: Int 															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
displayPopup:function(_title, _height, _width){
	AUI().use('aui-dialog', 'aui-io', 'event', 'event-custom', function(A) {

			DIALOG_POPUP = new A.Dialog({
            					title: _title,
            					centered: true,
            					draggable: true,
            					modal: true,
            					height: _height,
            					width: _width,
            					scrollbars: true,
            					bodyContent: '<button type="button" onclick="dialogId.close()" value="close"></button>'
        					}).plug(A.Plugin.IO, {uri:URL_POPUP}).render();
			DIALOG_POPUP.show();
			
 	 });
},
/*
 * 
 * Function: getMapWFSArray	 											   							
 * Parameters:
 * 		 Parameter1: Array 															 																				
 * Return:	 
 * 		 Array
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getMapWFSArray:function(_array){
	_arrayNew = new Array();
	for(var i=0; i < _array.length ; i++){
		_arrayNew[i] = map.layers[_array[i].IndexLayer];
	}
	return _arrayNew;
},

/*
 * 
 * Function: 		 											   							
 * Parameters:
 * 		 Parameter1: String (featureId)															 																				
 * Return:	 
 * 		 Object
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getLayerByID:function(_fid){
	var splitData = _fid.split('.');
	_layerName = splitData[0];
	var idLayer = null;
	var layer = null;
	for(var i=0; i< vectorArray.length ; i++ ){
		if(vectorArray[i].LayerId == _layerName && !vectorArray[i].Map_Service){
			idLayer = vectorArray[i].IndexLayer;
			break;
		}
	}
	if(idLayer == null) alert("This layer is not available !");
	else {
		layer = map.layers[idLayer];
	}
	return layer;	
},
/*
 * 
 * Function: getLayerByIdLayer	 											   							
 * Parameters:
 * 		 Parameter1: String 															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getLayerByIdLayer:function(_idLayer){
	//_layerName = _idLayer;
	var idLayer = null;
	for(var i=0; i< vectorArray.length ; i++ ){
		if(vectorArray[i].LayerId == _idLayer && vectorArray[i].SearchAttr){
			idLayer = vectorArray[i].IndexLayer;
			break;
		}
	}
	return map.layers[idLayer];
},
/*
 * 
 * Function: getFeatureById	 											   							
 * Parameters:
 * 		 Parameter1: Object 	
 * 	     Parameter2: String														 																				
 * Return:	 
 * 		 Object 
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getFeatureById:function (_layer,_fid){
	var feature = null;
	for(var i=0 ; i < _layer.features.length ; i++ ){
		if(_layer.features[i].fid == _fid){
			feature = _layer.features[i];
		}
	}
	return feature;
},
/*
 * 
 * Function: resetColorFeature	 											   							
 * Parameters:
 * 		 Parameter1: Array (list of featureId)										 																				
 * Return:	 
 * 		 Object 
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
resetColorFeature:function(_array){
	
	if(_array.length > 0)
	for(var i=0; i< _array.length ; i++ ){
		var _fid = _array[i];
		var splitData = _fid.split('.');
		var layerName = splitData[0];
		
		var layerTemp = CommonGeo.getLayerByIdLayer(layerName);
		var feature = CommonGeo.getFeatureById(layerTemp,_fid);
		layerTemp.drawFeature(feature, "default");
	}
},
/*
 * 
 * Function: setColorFeature	 											   							
 * Parameters:
 * 		 Parameter1: String (list of featureId)										 																				
 * Return:	 
 * 		 Object 
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
setColorFeature:function(_fid){
	var splitData = _fid.split('.');
	var layerName = splitData[0];
	var layerTemp = CommonGeo.getLayerByIdLayer(layerName);
	var feature = CommonGeo.getFeatureById(layerTemp,_fid);
	layerTemp.drawFeature(feature, "select");
},
/*
 * 
 * Function: getBoundByFid	 											   							
 * Parameters:
 * 		 Parameter1: String 															 																				
 * Return:	 
 * 		 String 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getBoundByFid:function(_fid){
	layer = CommonGeo.getLayerByID(_fid);	
	feature = CommonGeo.getFeatureById(layer,_fid);
	bound = feature.geometry.getBounds().clone();
	if(bound != null)
		return bound;
},
/*
 * 
 * Function: isPointInLine	 											   							
 * Parameters:
 * 		 Parameter1: Geometry 															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
isPointInLine:function(geometry){
	pointTemp = new OpenLayers.Geometry.Point();
	pointTemp.x = point.x;
	pointTemp.y = point.y;
	geom.containsPoint(pointTest);
	return true;
},
/*
 * 
 * Function: isPointInPolygon	 											   							
 * Parameters:
 * 		 Parameter1: Geometry
 * 		 Parameter2: Geometry 															 																				
 * Return:	 (void)  
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
isLineInPolygon:function(_coverGeom,_lineGeom){
	
	var lineTest = _lineGeom;
	var pointTest = null;
	var j=0;
	var k=0;
	// tao mang xem co nam trong hay ko
	var IsIn = [];	
	
	
	for(j=0;j<lineTest.components.length;j++)
	{
		var lineComponents = lineTest.components[j];
		IsIn[j] = [];
		
		for(k = 0; k < lineComponents.components.length; k++)
		{
			IsIn[j][k] = false;
		}
	}
	for(j=0; j<lineTest.components.length; j++)
	{
		var lineComponents = lineTest.components[j];
		
		for(k=0;k<lineComponents.components.length;k++)
		{
			pointTest = lineComponents.components[k];
			if(_coverGeom.containsPoint(pointTest))
			{
				IsIn[j][k] = true;
				//break;
			}
		}
	}
	var bIsIn = true;
	for(var i=0;i<IsIn.length;i++)
	{
		for(k=0;k<IsIn[i].length;k++)
		{
			if(!IsIn[i][k])
			{
				bIsIn = false;
				break;
			}
		}
		if(!bIsIn) break;
	}
	return bIsIn;
	
},
/*
 * 
 * Function: getTypeOfGeometry	 											   							
 * Parameters:
 * 		  Parameter1: String 	 															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	get type of geometry of layer
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
getTypeOfGeometry: function(_layerId){
	
	var GeometryType = null;
	for(var i=0 ; i< vectorArray.length ; i++){
		if(vectorArray[i].SearchAttr){
			if(vectorArray[i].LayerId == _layerId)
				{
					GeometryType = vectorArray[i].Geometry_Type;
					break;
				}
		}		
	}
	return GeometryType;
	
},
/*
 * 
 * Function: setCurrentAlias	 											   							
 * Parameters:
 * 		 void 															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	
 * 
 * Author: NGUYENNV
 * Date : 2012/12/03										   												
 * 
 */
setCurrentAlias: function(_idlayer){
	var _arrayCurrent = new Array();
	for(var i=0; i < arrField_Alias.length;i++ )
	{
		if(_idlayer == arrField_Alias[i].LayerName)
			{
			_arrayCurrent[_arrayCurrent.length] = arrField_Alias[i];
			}
	}
	return _arrayCurrent;
	},
/*
 * 
 * Function: getAliasByColumName	 											   							
	 * Parameters:
	 * 		 void 															 																				
	 * Return:	 
	 * 		 void : 
	 * 
	 * Description:	
	 * 
	 * Author: NGUYENNV
	 * Date : 2012/12/03										   												
	 * 
	 */
getAliasByColumName: function(_array, _columnName){
	
		for(var i=0; i < _array.length;i++ )
		{
			if(_columnName == _array[i].Name)
				{
					return _array[i].Alias;
				}
		}
	},
/*
 * 
 * Function: getAttributeOfLayer	 											   							
	 * Parameters:
	 * 		 void 															 																				
	 * Return:	 
	 * 		 void : 
	 * 
	 * Description:	
	 * 
	 * Author: NGUYENNV
	 * Date : 2012/12/03										   												
	 * 
	 */
getAttributeOfLayer:function(_LayerID){
		var _array = new Array();
		var _currentAlias = new Array();
		_object = 
		{
		    attribute: null,

		    create: function () {
		    	this.attribute  =new Array();
		    	}
		};
		OBJECT = _object;
		var _layer = CommonGeo.getLayerByIdLayer(_LayerID);
		_currentAlias = CommonGeo.setCurrentAlias(_LayerID);
		//return _array;
		for(var i = 0;i < _layer.features.length; i++)
		{
		//var _object ;
		_object =  new  OBJECT.create(i, "","","","","","","","","","","","");
		for(var j = 0; j< _currentAlias.length; j++)
				{
							var  att = _currentAlias[j].Name;
							if(att == "fid")
								_object.attribute[att] = _layer.features[i].fid;
							else
							{
								var str = _layer.features[i].attributes[att];
							
								if(str == null) str = "";
								_object.attribute[att] = str;
							}
				}
		_array[i] = _object;
		}
		return _array;
	},
/*
 * 
 * Function: setAttributeToFidArray	 											   							
	 * Parameters:
	 * 		 void 															 																				
	 * Return:	 
	 * 		 void : 
	 * 
	 * Description:	
	 * 
	 * Author: NGUYENNV
	 * Date : 2012/12/03										   												
	 * 
	 */
setAttributeToFidArray:function(_array,_layerID){
		
		var _arrayTemp = new Array();
		_arrayTemp = CommonGeo.getAttributeOfLayer(_layerID);
		var _currentArray = new Array();
		for(var i = 0; i < _array.length; i++)
			{
				for(var j = 0; j < _arrayTemp.length; j++)
					{
						if(_array[i] == _arrayTemp[j].attribute["fid"])
							{
								_currentArray[i] =  _arrayTemp[j];
							}
					}
			}
		return _currentArray;
	}

};
/*	
 * END class CommonGeo 
 * NGUYENNV
 * 
 */
/**
 * 	bidding combobox commune
 */
CommonGis={
	/** bidding combobox commune when change district */	
	onChangeDistrict:function(){
		var comboDistrict=base.getElementById("comboDistrict");
		var comboCommune=base.getElementById("comboCommune");
		var value=comboDistrict.options[comboDistrict.selectedIndex].value;
		if (comboCommune.length > 0){
			 while(comboCommune.length > 0)
			  {
				 comboCommune.remove(comboCommune.length-1);
			  }
		}
		if(value > 0)
			Graphic.comboPhuongXa(comboCommune,value);
		comboCommune.selectedIndex = 0;
		$.uniform.update("#comboCommune");
	},
	/** combobox commune change */	
	onChangeCommune:function(){
		var commune=this.value;
		if(commune!="-1"){
			$("#select_category_matuyenduong option").each(function(){
				var title = $(this).attr("title");
				var val = $(this).val();
				if(val!="0"){
					if(title==commune)$(this).show();
					else $(this).hide();
				}
			});
		}else{
			$("#select_category_matuyenduong option").each(function(){
				if($(this).val!="0")$(this).show();
			});
		}
		$.uniform.update("#select_category_matuyenduong");
	},
	isLineInDisttrict:function(line){
		var lineTest = line;
		var pointTest = null;
		var i=0,j=0,k=0;
		var distId=null;
		for(i=0; i<arrFeatureDistrict.length; i++)
		{
			var geom = arrFeatureDistrict[i].geometry.components[0];
			for(j=0;j<lineTest.components.length;j++)
			{
				var lineComponents = lineTest.components[j];
				for(k=0;k<lineComponents.components.length;k++)
				{
					pointTest = lineComponents.components[k];
					if(geom.containsPoint(pointTest))
					{
						distId = arrFeatureDistrict[i].attributes["dist_id"];
						break;
					}
				}
			}
		}
		return distId;
	},
	isLineInCommune:function(line){
		var lineTest = line;
		var pointTest = null;
		var i=0,j=0,k=0;
		var commune=null;
		for(i=0; i<arrFeatureCommune.length; i++)
		{
			var geom = arrFeatureCommune[i].geometry.components[0];
			for(j=0;j<lineTest.components.length;j++)
			{
				var lineComponents = lineTest.components[j];
				for(k=0;k<lineComponents.components.length;k++)
				{
					pointTest = lineComponents.components[k];
					if(geom.containsPoint(pointTest))
					{
						commune = arrFeatureCommune[i].attributes["com_id"];
						break;
					}
				}
			}
		}
		return commune;
	},
	isLineInDaNang:function(line)
	{
		var lineTest = line;
		var pointTest = null;
		var i=0,j=0,k=0;
		var IsIn = [];	
		for(j=0;j<lineTest.components.length;j++)
		{
			var lineComponents = lineTest.components[j];
			IsIn[j] = [];
			for(k=0;k<lineComponents.components.length;k++)
			{
				IsIn[j][k] = false;
			}
		}
		
		for(i=0; i<arrFeatureDistrict.length; i++)
		{
			var geom = arrFeatureDistrict[i].geometry.components[0];
			for(j=0;j<lineTest.components.length;j++)
			{
				var lineComponents = lineTest.components[j];
				for(k=0;k<lineComponents.components.length;k++)
				{
					pointTest = lineComponents.components[k];
					if(geom.containsPoint(pointTest))
					{
						IsIn[j][k] = true;
					}
				}
			}
		}
		var bIsIn = true;
		for(i=0;i<IsIn.length;i++)
		{
			for(k=0;k<IsIn[i].length;k++)
			{
				if(!IsIn[i][k])
				{
					bIsIn = false;
					break;
				}
			}
			if(!bIsIn) break;
		}
		return bIsIn;
	},
	isPolygonInDaNang:function(poly)
	{
		var j=0;
		var bIsIn = false;
		for(j=0;j<poly.length;j++){
			var pPoint = new OpenLayers.Geometry.Point(poly[j].x,poly[j].y);
			var pointFeature = new OpenLayers.Feature.Vector(pPoint,null,null);
			var isIn = isPointInDaNang(pointFeature.geometry);
			if(!isIn){
				bIsIn = false;
				break;
			}else{
				bIsIn = true;
			}
		}
		return bIsIn;
	},
	isPointInCommune:function(_geoPoint,_idQuan){
		var IsIn = false;
		for(var i=0; i<arrFeatureCommune.length; i++)
		{
			if(arrFeatureCommune[i].attributes["com_id"] == _idQuan)
			{
				var geom = arrFeatureCommune[i].geometry.components[0];
				if(geom.containsPoint(_geoPoint))
				{
					IsIn = true;
					break;
				}
			}
		}
		return IsIn;
	},
	isLineInCommuneSearch:function(_lineGeom,_idQuanHuyen){
		var lineTest = _lineGeom;
		var pointTest = null;
		var j=0;
		var k=0;
		var bIsIn = false;
		var _featureDistrict = null;
		// tao mang xem co nam trong hay ko
		for(var i=0; i<arrFeatureCommune.length; i++)
		{
			if(arrFeatureCommune[i].attributes["com_id"] == _idQuanHuyen)
			{
				_featureDistrict = arrFeatureCommune[i];
			}
		}
		var geom = _featureDistrict.geometry.components[0];
		for(j=0; j<lineTest.components.length; j++)
		{
			var lineComponents = lineTest.components[j];
			for(k=0;k<lineComponents.components.length;k++)
			{
				pointTest = lineComponents.components[k];
				if(geom.containsPoint(pointTest))
				{
					bIsIn = true;
					break;
				}
			}
		}
		return bIsIn;
	},
	filterTuyenDuong:function(){
		if(actionCurrent == PortletTypeAction.Insert||actionCurrent==PortletTypeAction.Edit){
			var indexLayer = LayerTool.getIndexLayer();
			var geom = vectorArray[indexLayer].Geometry_Type;
			if(geom==Geometries.Point){
				if(featureCurrentAction!=null){
					var commune = CommonFuns.getCommunByGeom(featureCurrentAction.geometry);
					var select = document.getElementById("select_category_matuyenduong");
					if(select==null){
						window.setTimeout("LayerTool.drawFormInsertData()", 1000);
					}else{
						$("#select_category_matuyenduong option").each(function(){
							var title = $(this).attr("title");
							if(title==commune){
								$(this).show();
							}else{
								$(this).hide();
							}
						});
					}
				}
			}
		}
	},
	getFeatureById:function(assetID,column){
		var index = LayerTool.getIndexLayer();
		var layer = map.layers[index];
		var feature = null;
		var i=0;
		var arrAlias = [];
		var strCode = "";
		var layerId = vectorArray[index-numBaseLayer].LayerId;
		for (i = 0; i < arrField_Alias.length; i++) {
			if (base.trim(arrField_Alias[i].LayerName.toLowerCase()) == base.trim(layerId
					.toLowerCase())) {
				arrAlias[arrAlias.length] = arrField_Alias[i];
			}
		}
		arrAlias.sort(function(a, b) {
			var x = a.Index - b.Index;
			return x;
		});
		for(i=0;i<arrAlias.length;i++){
			if(arrAlias[i].IsRequire)strCode = strCode +arrAlias[i].Name+",";
		}
		var split = strCode.split(",");
		var data = "0";
		if(arrAssets.length>0){
			for(i=0;i<arrAssets.length;i++){
				var asset = arrAssets[i];
				if(asset==assetID){
					for(var j=0;j<layer.features.length;j++){
						var feature = layer.features[j];
						if(feature.attributes[split[0]]==asset){
							data = feature.attributes[column];
							break;
						}
					}
					break;
				}
			}
		}else{
			for(i=0;i<layer.features.length;i++){
				var feature = layer.features[i];
				if(feature.attributes[split[0]]==assetID){
					data = feature.attributes[column];
					break;
				}
			}
		}
		return data;
	},
	actionFileLink:function(){
		var link = "";
		if(actionCurrent == PortletTypeAction.Insert)
			link = $("#url_link_asset").attr("link");
		else if(actionCurrent == PortletTypeAction.Identify)
			link = $("#url_link_asset_identify").attr("lang");
		window.open(link);
	},
	getFeatureName:function(fid){
		var index = LayerTool.getIndexLayer();
		var layer = map.layers[index];
		var result = "";
		if(layer!=null){
			for(var i=0;i<layer.features.length;i++){
				var data = layer.features[i].attributes["fid"];
				if(data==fid){
					result = layer.features[i].attributes[objDelName];
					break;
				}
			}
		}
		return result;
	}
};
/**
 * 		get number day of month
 * @param year	Year
 * 		@param month	Month
 * 		@returns day	Number day of month
 */
function daysOfMonth(year,month) {
    return new Date(year, month, 0).getDate();
}
/**
 * 		check number
 * @param numCheck	Number want to check
 * @returns	true/false (true: is number, false: not number)
 */
function isNumeric(input){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(input));
}
/**
 * 	check date type (dd/MM/yyyy)
 * @param data	Data check
 * @returns	true/false (true: is type date, false: not type date)
 */
function checkDateType(data){
	var regExp = /^\d{2}\/\d{2}\/\d{4}$/;
	return regExp.test(data);
}

/**
 * 	check date type (yyyy-MM-dd)
 * @param data Data check
 * @returns true/fase (true: is type date,false: not type date)
 */
function validDate(data){
	var re= /^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/;
	return re.test(data);
}


function isNumberKey2(evt)
{
   var charCode = (evt.which) ? evt.which : event.keyCode;
   if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)){
  	 return false;
   }
   return true;
}
/**
 * 		get commune id contain polygon
 * @param polygon	polygon want get commune id
 * @returns	commune id
 */
function getDistrictContainPolygon(polygon){
	var bound = polygon.geometry.getBounds();
	var distID = null;
	for (var i = 0; i < arrFeatureDistrict.length; i++) {
		var featureDistrict = arrFeatureDistrict[i];
		var b = featureDistrict.geometry.bounds;
		if (bound.intersectsBounds(b)) {
			distID = featureDistrict.attributes["dist_id"];
			break;
		}
	}
	return distID;
}
/**
 * 		get district id contain polygon
 * @param polygon	polygon want get district id
 * @returns	district id
 */
function getCommuneContainPolygon(polygon){
	var bound = polygon.geometry.getBounds();
	var distID = null;
	for (var i = 0; i < arrFeatureCommune.length; i++) {
		var featureCommune = arrFeatureCommune[i];
		var b = featureCommune.geometry.bounds;
		if (bound.intersectsBounds(b)) {
			distID = featureCommune.attributes["com_id"];
			break;
		}
	}
	return distID;
}
function checkValue(stringDateParam) {
	var arr = ["$","?","!","@","*","%","#","^","&","|","+","-","[","]","{","}",";","'",'"',];
	var bool = false;
	for(var i=0;i<arr.length;i++){
		if(stringDateParam.contains(arr[i]))
		{
			bool = true;
			break;
		}
	}
	return bool;
}

function checkIdSymboy(value){
	var arr = ["$","?","!","@","*","%","#","^","&","|","+","-","[","]","{","}",";","'",'"',"/"," "];
	var bool = false;
	for(var i=0;i<arr.length;i++){
		if(value.contains(arr[i]))
		{
			bool = true;
			break;
		}
	}
	return bool;
}

function checkIntegerNumber(number){
	var reg = /^-?\d+\.?\d*$/;
	return reg.test(number);
}

function checkDateBeetwen(from,to,check){
	var fDate,lDate,cDate;
    fDate = Date.parse(from);
    lDate = Date.parse(to);
    cDate = Date.parse(check);
    if((cDate <= lDate && cDate >= fDate)) {
        return true;
    }
    return false;
}

function getFeatureById(assetID){
	var index = getIndexLayerCurrent();
	var layer = map.layers[index];
	var feature = null;
	var i=0;
	var arrAlias = [];
	var strCode = "";
	var layerId = vectorArray[index-numBaseLayer].LayerId;
	for (i = 0; i < arrField_Alias.length; i++) {
		if (base.trim(arrField_Alias[i].LayerName.toLowerCase()) == base.trim(layerId
				.toLowerCase())) {
			arrAlias[arrAlias.length] = arrField_Alias[i];
		}
	}
	arrAlias.sort(function(a, b) {
		var x = a.Index - b.Index;
		return x;
	});
	for(i=0;i<arrAlias.length;i++){
		if(arrAlias[i].IsRequire)strCode = strCode +arrAlias[i].Name+",";
	}
	var split = strCode.split(",");
	for(i=0;i<layer.features.length;i++){
		var feat = layer.features[i];
		if(feat.attributes[split[0]]==assetID){
			feature = layer.features[i];
			break;
		}
	}
	return feature;
}

function getFeatureByIdAndLayer(assetId,layerId){
	var index = 0;
	var sLayerId = "";
	for(var i=0;i<vectorArray.length;i++){
		if(vectorArray[i].LayerId==layerId){
			if(!vectorArray[i].Map_Service){
				index = vectorArray[i].IndexLayer;
				sLayerId = vectorArray[i].LayerId;
			}
		}
	}
	var layer = map.layers[index];
	var feature = null;
	var i=0;
	var arrAlias = [];
	var strCode = "";
	for (i = 0; i < arrField_Alias.length; i++) {
		if (base.trim(arrField_Alias[i].LayerName.toLowerCase()) == base.trim(sLayerId
				.toLowerCase())) {
			arrAlias[arrAlias.length] = arrField_Alias[i];
		}
	}
	arrAlias.sort(function(a, b) {
		var x = a.Index - b.Index;
		return x;
	});
	for(i=0;i<arrAlias.length;i++){
		if(arrAlias[i].IsRequire)strCode = strCode +arrAlias[i].Name+",";
	}
	var split = strCode.split(",");
	for(i=0;i<layer.features.length;i++){
		var feat = layer.features[i];
		if(feat.attributes[split[0]]==assetId){
			feature = layer.features[i];
			break;
		}
	}
	return feature;
}

function checkDateAfter(date1,date2){
	var splLD = date1.split("/");
	var splSD = date2.split("/");
	var timeStart=new Date(splLD[2],splLD[1],splLD[0],0,0,0,0);
	var timeStop=new Date(splSD[2],splSD[1],splSD[0],0,0,0,0);
	var check = false;
	if(timeStart.getTime()>timeStop.getTime()){
		check = true;
	}
	return check;
}

function checkDateBetweenTwoDates(from,to,check) {
	var splFrom = from.split("-");
	var splTo = to.split("-");
	var splCheck = check.split("-");
	var fDate = new Date(splFrom[0],splFrom[1]-1,splFrom[2]);
	var tDate = new Date(splTo[0],splTo[1]-1,splTo[2]);
	var cDate = new Date(splCheck[0],splCheck[1]-1,splCheck[2]);
	var bCheck = false;
	if(cDate>=fDate&&cDate<=tDate)bCheck=true;
	return bCheck;
}
function sortCombo(comboControl,idCombo){
	$(comboControl).each(function() {
		var selectedValue = $(this).val();
		$(this).html($("option", $(this)).sort(function(a, b) {
			return a.text.toLowerCase() == b.text.toLowerCase() ? 0 : a.text.toLowerCase() < b.text.toLowerCase() ? -1 : 1;
		}));
		$(this).val(selectedValue);
	});
}

function NASort(a, b) {    
	if (a.value != '0'||a.value != '-1') {
	        return -1;   
    }
    else if (b.value == '0'||b.value == '-1') {
        return 1;   
    }       
	return (a.value > b.value) ? 1 : -1;
};

function txtKeyDown(event) {
	// Allow: backspace, delete, tab, escape, and enter
	if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9
			|| event.keyCode == 27 || event.keyCode == 13 ||
			// Allow: Ctrl+A
			(event.keyCode == 65 && event.ctrlKey === true) ||
			// Allow: home, end, left, right
			(event.keyCode >= 35 && event.keyCode <= 39)) {
		// let it happen, don't do anything
		return;
	} else if (event.keyCode == 190) {
		var sText = this.value;
		var isDecimal = this.IsDecimal;
		if ((sText.indexOf('.') == -1) && (isDecimal == 1)
				&& (sText.length > 0))
			return;
		else
			event.preventDefault();
	} else {
		// Ensure that it is a number and stop the keypress
		if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57)
				&& (event.keyCode < 96 || event.keyCode > 105)) {
			event.preventDefault();
		}
	}
}
/**
 * 		set change text attribute 
 * 	event change text of text box
 */
function setChangedTextAttribute() {
	bSaveEditting = false;
	if(featureExtent!=null){
		var val = this.value;
		if(this.IsDecimal==1){
			if(val==""||val.length==0)val=0.0;
		}else if(this.IsDecimal==0){
			if(val==""||val.length==0)val=0;
		}
		if (featureExtent.feature)
			featureExtent.feature.attributes[this.attribute] = val;
		else
			featureExtent.attributes[this.attribute] = val;
	}else{
		if (featureCurrentAction == null) {
			return;
		}
		var val = this.value;
		if(this.IsDecimal==1){
			if(val==""||val.length==0)val=0.0;
		}else if(this.IsDecimal==0){
			if(val==""||val.length==0)val=0;
		}
		if (featureCurrentAction.feature) 
			featureCurrentAction.feature.attributes[this.attribute] = val;
		else 
			featureCurrentAction.attributes[this.attribute] = val;		
		if(this.name.match("matrambom")){
			$("#hiddenMaTramBom").val(val);
			ObjMayBom.maTramBom=val;
		}
		if(this.name.match("tentrambom")){
			ObjMayBom.tenTramBom=val;
		}
		if(this.name.match("manmxl")){
			MayPhatDien.maNhaMay = val;
		}
	}
}

function onKeyPressLink(){
	var value =  document.getElementById("<portlet:namespace/>txt_Map_AddFeature_Attribute_lienket").value;
	if(featureExtent!=null){
		if (featureExtent.feature)
			featureExtent.feature.attributes[this.attribute] = value;
		else
			featureExtent.attributes[this.attribute] = value;
	}else{
		if (featureCurrentAction == null) {
			return;
		}
		if (featureCurrentAction.feature)
			featureCurrentAction.feature.attributes[this.attribute] = value;
		else
			featureCurrentAction.attributes[this.attribute] = value;
	}
	if(value.length==0){
		$("#url_link_asset").hide();
	}else{
		if(checkURL_Asset(value)){
			$("#url_link_asset").show();
			$("#url_link_asset").attr("link",value);			
		}else{
			$("#url_link_asset").hide();
		}
	}
}

function checkURL_Asset(value) {
    var urlregex = new RegExp("^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");
    if (urlregex.test(value)) {
        return (true);
    }
    return (false);
}


/**
 * 		set change combobox attribute 
 * 	event change select of combobox
 */
function setChangedComboBoxAttribute() {
	bSaveEditting = false;
	var objValue = this[this.selectedIndex].value;
	if(objValue == 0)
		objValue = "";
	if(featureExtent!=null){
		if (featureExtent.feature) {
			featureExtent.feature.attributes[this.attribute] = objValue;
		} else {
			featureExtent.attributes[this.attribute] = objValue;
		}
	}else{
		if (featureCurrentAction == null) {
			return;
		}
		if (featureCurrentAction.feature) {
			featureCurrentAction.feature.attributes[this.attribute] = objValue;
		} else {
			featureCurrentAction.attributes[this.attribute] = objValue;
		}
		if(this.attribute.match("soluong")){
			var layer = LayerTool.getLayerIdSelected();
			if(layer==LayerAssetName.nhaMayXL){
				MayPhatDien.numberSelect = this[this.selectedIndex].value;
				MayPhatDien.showDialogSelect();
			}else{
				ObjMayBom.drawFormCreate(objValue);				
			}
		}
	}
}

$( ".checkma" ).keydown(function( e ) {
	var event = e;
	 if(event.shiftKey)
	  {
	   if ((event.keyCode < 65 || event.keyCode > 90) 
	        && (event.keyCode < 97 || event.keyCode > 122) 
	        //Allow: _ on Firefox (173) and Chrome (189)
	        && event.keyCode != 173 && event.keyCode != 189 
	        //Allow: Shift + Home, Shift + End
	        && event.keyCode != 36 && event.keyCode != 35 ) {
	                    event.preventDefault();
	                }
	   return;
	  }
	 //alert(event.keyCode);
	  // Allow: backspace, delete, tab, escape, and enter
	     var event = e;
	        if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9
	      || event.keyCode == 27 || event.keyCode == 13 || 
	        // Allow: Ctrl+A
	      (event.keyCode == 65 && event.ctrlKey === true) || 
	        // Allow: home, end, left, right
	      (event.keyCode >= 35 && event.keyCode <= 39)) {
	            // let it happen, don't do anything
	            return;
	        }
	        else
	     {      
	         //event.shiftKey ||          
	      if ((event.keyCode < 48 || event.keyCode > 57) 
	        && (event.keyCode < 65 || event.keyCode > 90) 
	        && (event.keyCode < 97 || event.keyCode > 122)) {
	                    event.preventDefault();
	                }
	     }
	});

$( ".checknumber" ).keydown(function( event ) {
	// Allow: backspace, delete, tab, escape, and enter
	if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9
			|| event.keyCode == 27 || event.keyCode == 13 ||
			// Allow: Ctrl+A
			(event.keyCode == 65 && event.ctrlKey === true) ||
			// Allow: home, end, left, right
			(event.keyCode >= 35 && event.keyCode <= 39)) {
		// let it happen, don't do anything
		return;
	}
	// Allow: .
	else if (event.keyCode == 190||event.keyCode == 110) {
		var sText = this.value;
		var isDecimal = 1;
		if ((sText.indexOf('.') == -1) && (isDecimal == 1)
				&& (sText.length > 0))
			return;
		else
			event.preventDefault();
	} else {
		// Ensure that it is a number and stop the keypress
		if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57)
				&& (event.keyCode < 96 || event.keyCode > 105)) {
			event.preventDefault();
		}
	}
});

$( ".checkintnumber" ).keydown(function( event ) {
	// Allow: backspace, delete, tab, escape, and enter
	if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9
			|| event.keyCode == 27 || event.keyCode == 13 ||
			// Allow: Ctrl+A
			(event.keyCode == 65 && event.ctrlKey === true) ||
			// Allow: home, end, left, right
			(event.keyCode >= 35 && event.keyCode <= 39)) {
		// let it happen, don't do anything
		return;
	}
	// Allow: .
	else if (event.keyCode == 190) {
		var sText = this.value;
		var isDecimal = 0;
		if ((sText.indexOf('.') == -1) && (isDecimal == 1)
				&& (sText.length > 0))
			return;
		else
			event.preventDefault();
	} else {
		// Ensure that it is a number and stop the keypress
		if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57)
				&& (event.keyCode < 96 || event.keyCode > 105)) {
			event.preventDefault();
		}
	}
});


function getFieldColumnKey(layerId){
	var column = null;
	switch (layerId) {
		case LayerAssetName.cauTrucChuyenDong:
			column = LayerFieldId.cauTrucCT;
			break;
		case LayerAssetName.cong:
			column = LayerFieldId.ongCong;
			break;	
		case LayerAssetName.diemKhachHang:
			column = LayerFieldId.diemKH;
			break;	
		case LayerAssetName.diemXa:
			column = LayerFieldId.diemXa;	
			break;
		case LayerAssetName.dayCapDiem:
			column = LayerFieldId.dayDien;	
			break;
		case LayerAssetName.giengTham:
			column = LayerFieldId.giengTham;	
			break;
		case LayerAssetName.hoDieuHoa:
			column = LayerFieldId.hoDieuHoa;	
			break;
		case LayerAssetName.hoga:
			column = LayerFieldId.hoGa;
			break;
		case LayerAssetName.kenh:
			column = LayerFieldId.kenh;
			break;
		case LayerAssetName.khuVucNgap:
			column = LayerFieldId.ngapUng;
			break;
		case LayerAssetName.luuVuc:
			column = LayerFieldId.luuVuc;
			break;	
		case LayerAssetName.nhaMayXL:
			column = LayerFieldId.nhaMayXuLy;
			break;	
		case LayerAssetName.phanhoiKH:
			column = LayerFieldId.phanhoiKH;
			break;	
		case LayerAssetName.tramBienAp:
			column = LayerFieldId.tramBA;
			break;
		case LayerAssetName.trambom:
			column = LayerFieldId.tramBom;
			break;
		case LayerAssetName.ongThoatNuoc:
			column = LayerFieldId.ongThoatNuoc;
			break;
		case LayerAssetName.suCo:
			column = LayerFieldId.suCo;
			break;	
		default:
			column = null;
			break;
	}
	return column;
}

function getFieldColumnName(layerId){
	var column = null;
	switch (layerId) {
		case LayerAssetName.cauTrucChuyenDong:
			column = LayerFieldId.cauTrucCT;
			break;
		case LayerAssetName.cong:
			column = LayerFieldName.ongCong;
			break;	
		case LayerAssetName.diemKhachHang:
			column = LayerFieldName.diemKH;
			break;	
		case LayerAssetName.diemXa:
			column = LayerFieldName.diemXa;	
			break;
		case LayerAssetName.dayCapDiem:
			column = LayerFieldName.dayDien;	
			break;
		case LayerAssetName.giengTham:
			column = LayerFieldName.giengTham;	
			break;
		case LayerAssetName.hoDieuHoa:
			column = LayerFieldName.hoDieuHoa;	
			break;
		case LayerAssetName.hoga:
			column = LayerFieldName.hoGa;
			break;
		case LayerAssetName.kenh:
			column = LayerFieldName.kenh;
			break;
		case LayerAssetName.khuVucNgap:
			column = LayerFieldName.ngapUng;
			break;
		case LayerAssetName.luuVuc:
			column = LayerFieldName.luuVuc;
			break;	
		case LayerAssetName.nhaMayXL:
			column = LayerFieldName.nhaMayXuLy;
			break;	
		case LayerAssetName.phanhoiKH:
			column = LayerFieldName.phanhoiKH;
			break;	
		case LayerAssetName.tramBienAp:
			column = LayerFieldName.tramBA;
			break;
		case LayerAssetName.trambom:
			column = LayerFieldName.tramBom;
			break;
		case LayerAssetName.ongThoatNuoc:
			column = LayerFieldName.ongThoatNuoc;
			break;
		case LayerAssetName.suCo:
			column = LayerFieldName.suCo;
			break;	
		default:
			column = null;
			break;
	}
	return column;
}

function getGeometryType(layerId){
	var column = null;
	switch (layerId) {
		case LayerAssetName.cauTrucChuyenDong:
			column = Geometries.Point;
			break;
		case LayerAssetName.cong:
			column = Geometries.Polyline;
			break;	
		case LayerAssetName.diemKhachHang:
			column = Geometries.Point;
			break;	
		case LayerAssetName.diemXa:
			column = Geometries.Point;	
			break;
		case LayerAssetName.dayCapDiem:
			column = Geometries.Polyline;	
			break;
		case LayerAssetName.giengTham:
			column = Geometries.Point;	
			break;
		case LayerAssetName.hoDieuHoa:
			column = Geometries.Polygon;	
			break;
		case LayerAssetName.hoga:
			column = Geometries.Point;
			break;
		case LayerAssetName.kenh:
			column = Geometries.Polyline;
			break;
		case LayerAssetName.khuVucNgap:
			column = Geometries.Polygon;
			break;
		case LayerAssetName.luuVuc:
			column = Geometries.Polygon;
			break;	
		case LayerAssetName.nhaMayXL:
			column = Geometries.Point;
			break;	
		case LayerAssetName.phanhoiKH:
			column = Geometries.Point;
			break;	
		case LayerAssetName.tramBienAp:
			column = Geometries.Point;
			break;
		case LayerAssetName.trambom:
			column = Geometries.Point;
			break;
		case LayerAssetName.ongThoatNuoc:
			column = Geometries.Polyline;
			break;	
		default:
			column = null;
			break;
	}
	return column;
}

function getLayerAlias(layerId){
	var column = null;
	switch (layerId) {
		case LayerAssetName.cauTrucChuyenDong:
			column = "";
			break;
		case LayerAssetName.cong:
			column = Geometries.Polyline;
			break;	
		case LayerAssetName.diemKhachHang:
			column = Geometries.Point;
			break;	
		case LayerAssetName.diemXa:
			column = LANGUAGE["asset_diemxa"];	
			break;
		case LayerAssetName.dayCapDiem:
			column = Geometries.Polyline;	
			break;
		case LayerAssetName.giengTham:
			column = LANGUAGE["asset_gieng"];	
			break;
		case LayerAssetName.hoDieuHoa:
			column = Geometries.Polygon;	
			break;
		case LayerAssetName.hoga:
			column = LANGUAGE["asset_hoga"];
			break;
		case LayerAssetName.kenh:
			column = Geometries.Polyline;
			break;
		case LayerAssetName.khuVucNgap:
			column = Geometries.Polygon;
			break;
		case LayerAssetName.luuVuc:
			column = Geometries.Polygon;
			break;	
		case LayerAssetName.nhaMayXL:
			column = LANGUAGE["asset_nmxl"];
			break;	
		case LayerAssetName.phanhoiKH:
			column = Geometries.Point;
			break;	
		case LayerAssetName.tramBienAp:
			column = LANGUAGE["asset_tba"];
			break;
		case LayerAssetName.trambom:
			column = LANGUAGE["asset_trambom"];
			break;
		default:
			column = null;
			break;
	}
	return column;
}

function InsertLog(chucnangid, moduleid, hoatdong){
	$.ajax({
		type:"POST",
		url:"/NhaTrangGis-portlet/html/drainwater/AjaxLog.jsp",
		data:{
			attr: "insertlog",
			chucnangid : chucnangid,
			moduleid: moduleid,
			hoatdong: hoatdong,
			username: $('#hidden_username').val()
	    },
		async:false,
		success:function(data){
			
		}
	});
}

function DeleteLog(logid){
	$.ajax({
		type:"POST",
		url:"/NhaTrangGis-portlet/html/drainwater/AjaxLog.jsp",
		data:{
			attr: "deletelog",
			logid: logid
	    },
		async:false,
		success:function(data){
			
		}
	});
}

function getPathJspFile(){
	var path = "";
	switch (INDEX_MODULE) {
		case ListModule.viewKhoangSan:
			path = sPortletJSP;
			break;
		case ListModule.quanlyDQT:
			path = sPortletJSP;
		default:
			break;
	}
	return path;
}

var arrInputControl = [];
var arrInputCoor = [];

function txtKeyDownHoSo(event) {
	// Allow: backspace, delete, tab, escape, and enter
	if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9
			|| event.keyCode == 27 || event.keyCode == 13 ||
			// Allow: Ctrl+A
			(event.keyCode == 65 && event.ctrlKey === true) ||
			// Allow: home, end, left, right
			(event.keyCode >= 35 && event.keyCode <= 39)) {
		// let it happen, don't do anything
		return;
	} else if (event.keyCode == 190||event.keyCode == 110) {
		var sText = this.value;
		if ((sText.indexOf('.') == -1) && (sText.length > 0))
			return;
		else
			event.preventDefault();
	} else {
		// Ensure that it is a number and stop the keypress
		if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57)
				&& (event.keyCode < 96 || event.keyCode > 105)) {
			event.preventDefault();
		}
	}
}


